<template>
  <div>
    <tableComponents
      :isShow="false"
      :isHaveExport="false"
      :isHaveAdd="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list"
          header-row-class-name="head_color"
          stripe
          v-loading="loading"
          tooltip-effect="dark"
          style="width: 99.9%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="插件名称" min-width="100">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="插件类别" min-width="100">
            <template slot-scope="scope">{{ scope.row.cate }}</template>
          </el-table-column>

          <el-table-column label="价格" min-width="50">
            <template slot-scope="scope">{{ scope.row.price|keepTwoNum }}</template>
          </el-table-column>

          <el-table-column label="简介" min-width="250">
            <template slot-scope="scope">{{ scope.row.desc }}</template>
          </el-table-column>

          <el-table-column label="是否开通" min-width="100" align="center">
            <template slot-scope="scope">
              <switchc
                :value="scope.row.is_open"
                @change="handleSwitch($event,scope.row.id)"
                text="on|off"
              ></switchc>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="showEditDialog(scope.row.id)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                @click="deleteNews( scope.row.id)"
                type="danger"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <PluginEdit @parent="reload" :show.sync="myshow" :editId="editId" :clickNum="clickNum"></PluginEdit>
  </div>
</template>
<script>
import switchc from "@/components/currency/Swich";
import HeadTitle from "@/components/currency/HeadTitle.vue";
import PluginEdit from "@/components/admin/pluginEdit.vue";

export default {
  data() {
    return {
      loading: false,
      show: false,
      myshow: false,
      list: [],
      clickNum: 1,
      editId: ""
    };
  },
  components: {
    switchc,
    HeadTitle,
    PluginEdit
  },
  methods: {

    getNewsAll() {
      this.loading = true;
      var data = {};
      this.$api.getAdminuser.getPluginList(data).then(res => {
        if (res.data.code) {
          console.log(res);
          this.list = res.data.result;
        }
      });
      this.loading = false;
    },
    handleSwitch(val, id) {
      var params = {};
      params["id"] = id;
      val == true ? (params["is_open"] = 1) : (params["is_open"] = 0);
      this.$api.getAdminuser.getopen(params).then(res => {
        console.log("!!", res);
        this.getNewsAll();
      });
    },
    deleteNews(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        console.log(willDelete, 1);
        if (willDelete.value) {
          var data = { id };
          this.$api.getAdminuser.PluginDelete(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll("");
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    showEditDialog(id) {
      this.myshow = true;
      this.editId = id;
      this.clickNum++;
    },
    reload(id) {
      this.getNewsAll();
    },
    handleSelectionChange() {}
  },
  beforeCreate() {
    this.loading = true; //数据未加载之前调用loading
  },
  created() {
    this.getNewsAll();
    this.loading = false;
  },
  computed: {},
  watch: {}
};
</script>
